package com.dang.dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import com.dang.dao.OrderDAO;
import com.dang.model.Order;
import com.dang.util.DBConnection;

public class OrderDAOImp implements OrderDAO{
	private static String SAVE="insert into d_order(id,user_id,status,order_time,order_desc,total_price,receive_name,full_address,postal_code,mobile,phone) values(?,?,?,?,?,?,?,?,?,?,?)";
	
	public void save(Order order)throws Exception{
		PreparedStatement prep=null;
		Connection conn=null;
		try{
			conn=DBConnection.getConnection();
			prep=conn.prepareStatement(SAVE,Statement.RETURN_GENERATED_KEYS);
			prep.setString(8, order.getFullAddress());
			prep.setString(10, order.getMobile());
			prep.setString(5, order.getOrderDesc());
			prep.setInt(1, order.getId());
			prep.setInt(2, order.getUserId());
			prep.setInt(3, order.getStatus());
			prep.setLong(4, order.getOrderTime());
			prep.setDouble(6, order.getTotalPrice());
			prep.setString(11, order.getPhone());
			prep.setString(7, order.getReceiveName());
			prep.setString(9, order.getPostalCode());
			prep.executeUpdate();
			ResultSet rst=prep.getGeneratedKeys();
			rst.next();
			int id=rst.getInt(1);
			order.setId(id);
		}finally{
			DBConnection.close(prep, conn);
		}
	}
}
